home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / util / gnu / unixcmds.lha / unixcmds / src / dirname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-06  |  1.3 KB  |  69 lines

  1. /* dirname - extract the directory name from a path     Author: Peter Holzer */
  2.  
  3. /* Dirname -- extract directory part from a path name
  4.  *
  5.  * Peter Holzer (hp@vmars.tuwien.ac.at)
  6.  *
  7.  * $Log: dirname.c,v $
  8.  * Revision 1.1  1994/02/12  16:15:02  hjp
  9.  * Initial revision
  10.  *
  11.  */
  12.  
  13. #include <string.h>
  14. #include <stdio.h>
  15.  
  16. int test_part(char p)
  17. {
  18.  return ((p == '/') || (p == ':'));
  19. }
  20.  
  21. int test_absolute(char *p)
  22. {
  23.  int tp;
  24.  
  25.  tp=test_part(p[0]);
  26.  
  27.  if (!tp)
  28.  {
  29.    tp=(strstr(p,":")!=NULL);
  30.  }
  31.  
  32.  return tp;
  33. }
  34.  
  35. int main(int argc, char **argv)
  36. /* [<][>][^][v][top][bottom][index][help] */
  37. {
  38.   char *p;
  39.   char *path;
  40.   int ac=1,amiga_mode=0;
  41.  
  42.   if (argc < 2) {
  43.         fprintf(stderr, "Usage: %s path\n", argv[0]);
  44.         return(1);
  45.   }
  46.  
  47.   if ((!strncmp(argv[1],"--amiga",7))&&(argc==3))
  48.   {
  49.     ac++;
  50.     amiga_mode=1;
  51.   }
  52.  
  53.   path = argv[ac];
  54.   p = path + strlen(path);
  55.   while (p > path && test_part(p[-1]) ) p--; /* trailing slashes */
  56.   while (p > path && (!test_part(p[-1])) ) p--; /* last component */
  57.   while (p > path && test_part(p[-1]) ) p--; /* trailing slashes */
  58.   if (p == path) {
  59. /*          printf(path[0] == '/' ? "/\n" : ".\n"); */
  60.         printf(test_absolute(path) ? ":\n" : "\"\"\n");
  61.  
  62.   } else {
  63.         printf("%.*s\n", (int) (p - path + amiga_mode), path);
  64.           
  65.   }
  66.   return(0);
  67. }
  68. /* [<][>][^][v][top][bottom][index][help] */
  69.